/*
 * GToe: Gtk+ TicTacToe
 * menu.c: game main menu
 *
 * Copyright (C) 2010 Dani El-Soufi <danisoufi@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "../include/core.h"
#include "../include/info.h"
#include "../include/menu.h"

MENU   *menu;
ITEM   **item;

int
get_menu_item(void)
{
    int c;

    while ((c = getch()) != KEY_ESC)
    {
        switch(c)
        {
            case KEY_DOWN:
                menu_driver(menu, REQ_DOWN_ITEM);
                break;
            case KEY_UP:
                menu_driver(menu, REQ_UP_ITEM);
                break;
            case KEY_ESC:
            	exit(0);
            	break;
            case 0xA: /* Return -> ASCII-Code */
                return item_index(current_item(menu));
        }
    	wrefresh(window);
    }
}

void
gtoe_about(void)
{
    int select;

    item = (ITEM **) calloc(2, sizeof(ITEM *));
    item[0] = new_item("Back to main menu" ,"");
    item[1] = 0;

    menu = new_menu(item);
    window = newwin(15, 68, (LINES / 8), (COLS / 12));
    set_menu_win(menu, window);
    set_menu_sub(menu, derwin(window, 1, 42, 13, 25));
    box(window, 0, 0);
    mvwprintw(window, 1, 23, "----- GToe-%s -----", VERSION);
    mvwprintw(window, 3, 5, "GToe is a classic TicTacToe game written for Linux and UNIX");
    mvwprintw(window, 4, 5, "and released under GPL license. It is possible to play it  ");
    mvwprintw(window, 5, 5, "against the computer or another player on the same PC. That");
    mvwprintw(window, 6, 5, "should be enough for you to have fun with (for now i hope).");
    mvwprintw(window, 9, 5, "Copyright %s 2010 %s %s    ", COPYRIGHT, AUTHOR, AUTHOR_MAIL);
    mvwprintw(window, 10, 5, "%s                        ", HOMEPAGE);
    set_menu_mark(menu, "");
    post_menu(menu);

    refresh();
    wrefresh(window);

    select = get_menu_item();

    if (select == 0)
    {
    	gtoe_select_menu();
    }
}

int
gtoe_select_menu(void)
{
    int select;

    clear();
    curs_set(0);

    item = (ITEM **) calloc(6, sizeof(ITEM *));
    item[0] = new_item("1." ,"Single player");
    item[1] = new_item("2.", "2 player (soon)");
    item[2] = new_item("3.", "Network (soon)");
    item[3] = new_item("4.", "About");
    item[4] = new_item("5." ,"Quit");
    item[5] = 0;

    menu = new_menu(item);
    window = newwin(10, 30, (LINES / 4), (COLS / 4));
    set_menu_win(menu, window);
    set_menu_sub(menu, derwin(window, 5, 28, 3, 2));
    box(window, 0, 0);
    mvwaddstr(window, 1, 7, "--- Main menu ---");
    set_menu_mark(menu, "   ");
    post_menu(menu);
    mvaddstr(LINES * 99/100, 0, "Press ESC to quit.");

    refresh();
    wrefresh(window);

    select = get_menu_item();

    switch(select)
    {
        case 0:
            return 1;
        case 1:
            return 2;
        case 2:
            gteo_select_menu();
        case 3:
            gtoe_about();
        case 4:
            exit(0);
    }
}

int
select_difficulty_menu(void)
{
    int select;

    item = (ITEM **) calloc(5, sizeof(ITEM *));
    item[0] = new_item("1." ,"Normal");
    item[1] = new_item("2.", "Hard");
    item[2] = new_item("3.", "Very hard");
    item[3] = new_item("4.", "Back to main menu");
    item[4] = 0;

    menu = new_menu(item);
    window = newwin(10, 30, (LINES / 4), (COLS / 4));
    set_menu_win(menu, window);
    set_menu_sub(menu, derwin(window, 5, 28, 3, 2));
    box(window, 0, 0);
    mvwaddstr(window, 1, 3, "--- Choose difficulty ---");
    set_menu_mark(menu, "   ");
    post_menu(menu);

    refresh();
    wrefresh(window);

    select = get_menu_item();

    switch(select)
    {
        case 0:
            return 1;
        case 1:
            return 2;
        case 2:
            return 3;
        case 3:
            gtoe_select_menu();
    }
}

int
select_network_menu(void)
{
    int select;

    item = (ITEM **) calloc(4, sizeof(ITEM *));
    item[0] = new_item("1." ,"Host");
    item[1] = new_item("2.", "Connect to host");
    item[2] = new_item("3.", "Back to main menu");
    item[3] = 0;

    menu = new_menu(item);
    window = newwin(10, 30, (LINES / 4), (COLS / 4));
    set_menu_win(menu, window);
    set_menu_sub(menu, derwin(window, 5, 28, 3, 2));
    box(window, 0, 0);
    mvwaddstr(window, 1, 5, "--- Network menu ---");
    set_menu_mark(menu, "   ");
    post_menu(menu);

    refresh();
    wrefresh(window);

    select = get_menu_item();

    switch(select)
    {
        case 0:
            return 1;
        case 1:
            return 2;
        case 2:
            gtoe_select_menu();
    }
}
